package bTree.LeetCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class 三数之和 {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (i != 0 && nums[i] == nums[i - 1]) continue;
            int left = i + 1;
            int right = nums.length - 1;
            while (left < right) {
                if (nums[left]==nums[left-1]&&i!=left-1) {
                    left++;
                    continue;
                }
                int temp = nums[i] + nums[left] + nums[right];
                if (temp==0){
                    List<Integer>path=new ArrayList<>();
                    path.add(nums[i]);
                    path.add(nums[left]);
                    path.add(nums[right]);
                    res.add(path);
                    left++;
                }else if (temp<0){
                    left++;
                }else {
                    right--;
                }
            }
        }
        return res;
    }
}